package site.shader85.Kingdoms.inventory;

import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.WeakHashMap;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_11_R1.inventory.CraftInventoryCustom;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.HumanEntity;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;

import net.minecraft.server.v1_11_R1.IInventory;
import site.shader85.Kingdoms.Utils;

public abstract class AbstractGUI extends CraftInventoryCustom {
	
	protected static Map<IInventory,AbstractGUI> views = new WeakHashMap<IInventory,AbstractGUI>();
	
	public AbstractGUI(int size,String name) {
		this(size,name,true);
	}
	
	public AbstractGUI(int size,String name,boolean initialize) {
		super(null, size,Utils.toMessage(name));
		if(initialize)
			refresh();
		views.put(inventory, this);
	}
	
	

	private ItemStack getSlot(int slot) {
		if (slot >= 0 && slot <= this.getSize() - 1)
			return index(slot);
		else
			return null;
	}
	
	public static AbstractGUI getGUI(IInventory inv){
		return views.get(inv);
	}
	
	public static void removeGUI(Object inv){
		views.remove(inv);
	}
	
	protected ItemStack create(Material m, String name, List<String> lore,boolean enchant) {
		ItemStack is = new ItemStack(m);
		ItemMeta im = Bukkit.getItemFactory().getItemMeta(m);
		if (name != null)
			im.setDisplayName(Utils.toMessage(name));
		if (lore != null) {
			ListIterator<String> iterator = lore.listIterator();
			while (iterator.hasNext())
				iterator.set(Utils.toMessage(iterator.next()));
			im.setLore(lore);
		}
		if(enchant){
			im.addEnchant(Enchantment.LUCK, 10, true);
		}
		im.addItemFlags(ItemFlag.HIDE_ENCHANTS);
		im.addItemFlags(ItemFlag.HIDE_UNBREAKABLE);
		im.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
		im.addItemFlags(ItemFlag.HIDE_PLACED_ON);
		im.addItemFlags(ItemFlag.HIDE_DESTROYS);
		is.setItemMeta(im);
		return is;
	}
	
	
	protected ItemStack createSkull(String name,String owner,List<String> lore){
		ItemStack is = new ItemStack(Material.SKULL_ITEM,1,(short)3);
		SkullMeta im = (SkullMeta)Bukkit.getItemFactory().getItemMeta(Material.SKULL_ITEM);
		im.setDisplayName(Utils.toMessage(name));
		im.setOwner(owner);
		if (lore != null) {
			ListIterator<String> iterator = lore.listIterator();
			while (iterator.hasNext())
				iterator.set(Utils.toMessage(iterator.next()));
			im.setLore(lore);
		}
		is.setItemMeta(im);
		im.addItemFlags(ItemFlag.HIDE_ENCHANTS);
		im.addItemFlags(ItemFlag.HIDE_UNBREAKABLE);
		im.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
		im.addItemFlags(ItemFlag.HIDE_PLACED_ON);
		im.addItemFlags(ItemFlag.HIDE_DESTROYS);
		return is;
	}
	
	protected abstract ItemStack index(int i);

	public abstract boolean onClick(int slot, ClickType type, HumanEntity player);
	
	public void refresh(){
		for (int i = 0; i < getSize(); i++) {
			setItem(i, getSlot(i));
		}
	}
}
